import { SettingAction } from '../actions'
import { Setting } from '../Types'
import { 
  Collapsed, 
  GetUserMenu_FULFILLED, 
  ChangeTopMenu, 
  InitCategory_FULFILLED, 
  InitBand_FULFILLED,
  InitCooper_FULFILLED,
  InitCenterCategory_FULFILLED,
  InitBaseInfo_FULFILLED,
  InitMinCategory_FULFILLED,
  InitErpShop_FULFILLED,
  InitVender_FULFILLED,
  InitEXPName_FULFILLED,
  InitBuyer_FULFILLED,
  InitStatus_FULFILLED,
  InitreStatus_FULFILLED,
  InitLAYOU_FULFILLED,
  InitColors_FULFILLED,
  InitSizes_FULFILLED,
  InitCheckList_FULFILLED,
  GetSOCRELIST_FULFILLED } from '../Constants'
// 初始化
const initState= {
  loading: false,
  collapsed: false,
  topMenuIndex: 0,
  menuData: [],
  categorys: [],
  bands: [],
  coopers: [],
  centerCategorys: [],
  minCategorys: [],
  baseInfo: [],
  erpShops: [],
  venders: [],
  expname:[],
  buyers: [],
  status: [],
  restatus:[],
  layou:[],
  styles: [],
  colors: [],
  sizes: [],
  photoChecks: [],
  scores: []
}
export function setting(state: Setting = initState, action: SettingAction): Setting {
  switch (action.type) {
    case Collapsed:
      return { ...state, collapsed: !state.collapsed }
    case GetUserMenu_FULFILLED:
      return { ...state, menuData: parseMenu(action.payload)}
    case ChangeTopMenu:
      return { ...state, topMenuIndex: action.payload }
    case InitCategory_FULFILLED:
      return { ...state, categorys: action.payload }
    case InitBand_FULFILLED: 
      return { ...state, bands: action.payload }
    case InitCooper_FULFILLED: 
      return { ...state, coopers: action.payload }
    case InitCenterCategory_FULFILLED: 
      return { ...state, centerCategorys: action.payload }
    case InitMinCategory_FULFILLED: 
      return { ...state, minCategorys: action.payload }
    case InitBaseInfo_FULFILLED: 
      return { ...state, baseInfo: action.payload }
    case InitErpShop_FULFILLED: 
      return { ...state, erpShops: action.payload }
    case InitVender_FULFILLED: 
      return { ...state, venders: action.payload }
    case InitEXPName_FULFILLED: 
      return { ...state, expname: action.payload }
    case InitBuyer_FULFILLED: 
      return { ...state, buyers: action.payload }
    case InitStatus_FULFILLED: 
      return { ...state, status: action.payload }
    case InitreStatus_FULFILLED: 
      return { ...state, restatus: action.payload }
      case InitLAYOU_FULFILLED: 
      return { ...state, layou: action.payload }
    case InitColors_FULFILLED: 
      return { ...state, colors: action.payload }
    case InitSizes_FULFILLED: 
      return { ...state, sizes: action.payload }
    case InitCheckList_FULFILLED: 
      return { ...state, photoChecks: action.payload }
    case GetSOCRELIST_FULFILLED: 
      return { ...state, scores: action.payload }
  }
  return state
}

function parseMenu(menus: Array<any>): Array<any> {
  let userMenus = new Array()
  if(!menus || !Array.isArray(menus)) return userMenus
  menus.map( (menu, index) => {
    userMenus = [...userMenus, {
      id: menu.Id,
      name: menu.name,
      icon: menu.icon,
      path: menu.path,
      children: parseMenu(menu.children)
    }]
  })
  return userMenus
}